// Package _5_合并两个排序的链表 main
// @Title _5_合并两个排序的链表
// @Description  输入两个递增排序的链表，合并这两个链表并使新链表中的节点仍然是递增排序的。
// @Author  == 琳琳也叫王木木 ==  2022-01-12 18:37
// @Create  2022-01-12 18:37
package main

import "fmt"

type ListNode struct {
	Val  int
	Next *ListNode
}

func mergeTwoLists(l1 *ListNode, l2 *ListNode) *ListNode {
	if l1 == nil {
		return l2
	}
	if l2 == nil {
		return l1
	}
	head := ListNode{}
	p := &head
	for l1 != nil && l2 != nil {
		if l1.Val <= l2.Val {
			p.Next = &ListNode{
				l1.Val,
				nil,
			}
			l1 = l1.Next
		} else {
			p.Next = &ListNode{
				l2.Val,
				nil,
			}
			l2 = l2.Next
		}
		p = p.Next
	}
	if l1 != nil {
		p.Next = l1
	}
	if l2 != nil {
		p.Next = l2
	}
	return head.Next
}

func main() {
	p1 := ListNode{
		0, &ListNode{
			2, &ListNode{
				4, &ListNode{
					6, nil,
				},
			},
		},
	}
	p2 := ListNode{
		1, &ListNode{
			3, &ListNode{
				5, &ListNode{
					7, nil,
				},
			},
		},
	}
	//mergeTwoLists(&p1, &p2)
	head := mergeTwoLists(&p1, &p2)
	for head != nil {
		fmt.Println(head.Val)
		head = head.Next
	}
}
